package com.example.xxpk.controller.users;

import com.example.xxpk.common.result.R;
import com.example.xxpk.pojo.Users;
import com.example.xxpk.service.users.IUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangguihong
 * @since 2025-01-21
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Api(tags="用户表")
public class UsersController {

    private final IUsersService usersService;

    @GetMapping("/userInfo")
    @ApiOperation("首页用户信息")
    public R userInfo(Integer userId)throws ParseException {
        return R.data(usersService.userInfo(userId));
    }

    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public R userUpdate(@RequestBody Users users)throws ParseException {
        return R.data(usersService.updateById(users));
    }

    @PostMapping("/userRanking")
    @ApiOperation("用户排名")
    public R userRanking(Integer userId)throws ParseException {
        List<Users> users = usersService.userRanking();
        Integer ranking=null;
        if(users!=null && users.size()!=0){
            for (int i = 0; i <users.size() ; i++) {
                if(users.get(i).equals(userId)){
                    ranking=i;
                    break;
                }
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("users",users);
        map.put("ranking",ranking);
        return R.data(map);
    }

}
